<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>noisynode.cpp 示例文件 | Qt Quick 5.11</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtquick-index.html">Qt Quick</a></td><td ><a href="qtquick-scenegraph-graph-example.html">Scene Graph - Graph</a></td><td >noisynode.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.1 参考指南</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">noisynode.cpp 示例文件</h1>
<span class="subtitle">scenegraph/graph/noisynode.cpp</span>
<!-- $$$scenegraph/graph/noisynode.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2017 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &quot;noisynode.h&quot;</span>

  <span class="preprocessor">#include &lt;QtCore/QRandomGenerator&gt;</span>
  <span class="preprocessor">#include &lt;QtQuick/QSGSimpleMaterialShader&gt;</span>
  <span class="preprocessor">#include &lt;QtQuick/QSGTexture&gt;</span>
  <span class="preprocessor">#include &lt;QtQuick/QQuickWindow&gt;</span>

  <span class="preprocessor">#define NOISE_SIZE 64</span>

  <span class="keyword">struct</span> NoisyMaterial
  {
      <span class="operator">~</span>NoisyMaterial() {
          <span class="keyword">delete</span> texture;
      }

      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color;
      <span class="type"><a href="qsgtexture.html">QSGTexture</a></span> <span class="operator">*</span>texture;
  };

  <span class="keyword">class</span> NoisyShader : <span class="keyword">public</span> <span class="type"><a href="qsgsimplematerialshader.html">QSGSimpleMaterialShader</a></span><span class="operator">&lt;</span>NoisyMaterial<span class="operator">&gt;</span>
  {
      QSG_DECLARE_SIMPLE_SHADER(NoisyShader<span class="operator">,</span> NoisyMaterial)

  <span class="keyword">public</span>:
      NoisyShader() {
          setShaderSourceFile(<span class="type"><a href="../qtgui/qopenglshader.html">QOpenGLShader</a></span><span class="operator">::</span>Vertex<span class="operator">,</span> <span class="string">&quot;:/scenegraph/graph/shaders/noisy.vsh&quot;</span>);
          setShaderSourceFile(<span class="type"><a href="../qtgui/qopenglshader.html">QOpenGLShader</a></span><span class="operator">::</span>Fragment<span class="operator">,</span> <span class="string">&quot;:/scenegraph/graph/shaders/noisy.fsh&quot;</span>);
      }

      <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">&gt;</span> attributes() <span class="keyword">const</span> override {  <span class="keyword">return</span> <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">&gt;</span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;aVertex&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;aTexCoord&quot;</span>; }

      <span class="type">void</span> updateState(<span class="keyword">const</span> NoisyMaterial <span class="operator">*</span>m<span class="operator">,</span> <span class="keyword">const</span> NoisyMaterial <span class="operator">*</span>) override {

          <span class="comment">// Set the color</span>
          program()<span class="operator">-</span><span class="operator">&gt;</span>setUniformValue(id_color<span class="operator">,</span> m<span class="operator">-</span><span class="operator">&gt;</span>color);

          <span class="comment">// Bind the texture and set program to use texture unit 0 (the default)</span>
          m<span class="operator">-</span><span class="operator">&gt;</span>texture<span class="operator">-</span><span class="operator">&gt;</span>bind();

          <span class="comment">// Then set the texture size so we can adjust the texture coordinates accordingly in the</span>
          <span class="comment">// vertex shader..</span>
          <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> s <span class="operator">=</span> m<span class="operator">-</span><span class="operator">&gt;</span>texture<span class="operator">-</span><span class="operator">&gt;</span>textureSize();
          program()<span class="operator">-</span><span class="operator">&gt;</span>setUniformValue(id_textureSize<span class="operator">,</span> <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span>(<span class="number">1.0</span> <span class="operator">/</span> s<span class="operator">.</span>width()<span class="operator">,</span> <span class="number">1.0</span> <span class="operator">/</span> s<span class="operator">.</span>height()));
      }

      <span class="type">void</span> resolveUniforms() override {
          id_texture <span class="operator">=</span> program()<span class="operator">-</span><span class="operator">&gt;</span>uniformLocation(<span class="string">&quot;texture&quot;</span>);
          id_textureSize <span class="operator">=</span> program()<span class="operator">-</span><span class="operator">&gt;</span>uniformLocation(<span class="string">&quot;textureSize&quot;</span>);
          id_color <span class="operator">=</span> program()<span class="operator">-</span><span class="operator">&gt;</span>uniformLocation(<span class="string">&quot;color&quot;</span>);

          <span class="comment">// We will only use texture unit 0, so set it only once.</span>
          program()<span class="operator">-</span><span class="operator">&gt;</span>setUniformValue(id_texture<span class="operator">,</span> <span class="number">0</span>);
      }

  <span class="keyword">private</span>:
      <span class="type">int</span> id_color <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>;
      <span class="type">int</span> id_texture <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>;
      <span class="type">int</span> id_textureSize <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>;
  };

  NoisyNode<span class="operator">::</span>NoisyNode(<span class="type"><a href="qquickwindow.html">QQuickWindow</a></span> <span class="operator">*</span>window)
  {
      <span class="comment">// Make some noise...</span>
      <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> image(NOISE_SIZE<span class="operator">,</span> NOISE_SIZE<span class="operator">,</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span><span class="operator">::</span>Format_RGB32);
      <span class="type"><a href="../qtcore/qtglobal.html#uint-typedef">uint</a></span> <span class="operator">*</span>data <span class="operator">=</span> (<span class="type"><a href="../qtcore/qtglobal.html#uint-typedef">uint</a></span> <span class="operator">*</span>) image<span class="operator">.</span>bits();
      <span class="keyword">for</span> (<span class="type">int</span> i<span class="operator">=</span><span class="number">0</span>; i<span class="operator">&lt;</span>NOISE_SIZE <span class="operator">*</span> NOISE_SIZE; <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="type"><a href="../qtcore/qtglobal.html#uint-typedef">uint</a></span> g <span class="operator">=</span> <span class="type"><a href="../qtcore/qrandomgenerator.html">QRandomGenerator</a></span><span class="operator">::</span>global()<span class="operator">-</span><span class="operator">&gt;</span>bounded(<span class="number">0xff</span>);
          data<span class="operator">[</span>i<span class="operator">]</span> <span class="operator">=</span> <span class="number">0xff000000</span> <span class="operator">|</span> (g <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="number">16</span>) <span class="operator">|</span> (g <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="number">8</span>) <span class="operator">|</span> g;
      }

      <span class="type"><a href="qsgtexture.html">QSGTexture</a></span> <span class="operator">*</span>t <span class="operator">=</span> window<span class="operator">-</span><span class="operator">&gt;</span>createTextureFromImage(image);
      t<span class="operator">-</span><span class="operator">&gt;</span>setFiltering(<span class="type"><a href="qsgtexture.html">QSGTexture</a></span><span class="operator">::</span>Nearest);
      t<span class="operator">-</span><span class="operator">&gt;</span>setHorizontalWrapMode(<span class="type"><a href="qsgtexture.html">QSGTexture</a></span><span class="operator">::</span>Repeat);
      t<span class="operator">-</span><span class="operator">&gt;</span>setVerticalWrapMode(<span class="type"><a href="qsgtexture.html">QSGTexture</a></span><span class="operator">::</span>Repeat);

      <span class="type"><a href="qsgsimplematerial.html">QSGSimpleMaterial</a></span><span class="operator">&lt;</span>NoisyMaterial<span class="operator">&gt;</span> <span class="operator">*</span>m <span class="operator">=</span> NoisyShader<span class="operator">::</span>createMaterial();
      m<span class="operator">-</span><span class="operator">&gt;</span>state()<span class="operator">-</span><span class="operator">&gt;</span>texture <span class="operator">=</span> t;
      m<span class="operator">-</span><span class="operator">&gt;</span>state()<span class="operator">-</span><span class="operator">&gt;</span>color <span class="operator">=</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span><span class="operator">::</span>fromRgbF(<span class="number">0.95</span><span class="operator">,</span> <span class="number">0.95</span><span class="operator">,</span> <span class="number">0.97</span>);
      m<span class="operator">-</span><span class="operator">&gt;</span>setFlag(<span class="type"><a href="qsgmaterial.html">QSGMaterial</a></span><span class="operator">::</span>Blending);

      setMaterial(m);
      setFlag(OwnsMaterial<span class="operator">,</span> <span class="keyword">true</span>);

      <span class="type"><a href="qsggeometry.html">QSGGeometry</a></span> <span class="operator">*</span>g <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsggeometry.html">QSGGeometry</a></span>(<span class="type"><a href="qsggeometry.html">QSGGeometry</a></span><span class="operator">::</span>defaultAttributes_TexturedPoint2D()<span class="operator">,</span> <span class="number">4</span>);
      <span class="type"><a href="qsggeometry.html">QSGGeometry</a></span><span class="operator">::</span>updateTexturedRectGeometry(g<span class="operator">,</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span>()<span class="operator">,</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span>());
      setGeometry(g);
      setFlag(OwnsGeometry<span class="operator">,</span> <span class="keyword">true</span>);
  }

  <span class="type">void</span> NoisyNode<span class="operator">::</span>setRect(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span> <span class="operator">&amp;</span>bounds)
  {
      <span class="type"><a href="qsggeometry.html">QSGGeometry</a></span><span class="operator">::</span>updateTexturedRectGeometry(geometry()<span class="operator">,</span> bounds<span class="operator">,</span> <span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>));
      markDirty(<span class="type"><a href="qsgnode.html">QSGNode</a></span><span class="operator">::</span>DirtyGeometry);
  }

</pre>
</div>
<!-- @@@scenegraph/graph/noisynode.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
